package org.hank.compose_yyzs.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import org.hank.compose_yyzs.bean.UnUploadFile
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.hank.compose_yyzs.bean.UnUploadInfo
import org.hank.compose_yyzs.db.dao.FileDao
import javax.inject.Inject

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/9/14 9:52
 *描述:
 */
@HiltViewModel
class UploadFileViewModel @Inject constructor(var fileDao: FileDao) : ViewModel() {
    var allFiles: MutableStateFlow<MutableList<UnUploadInfo>> = MutableStateFlow(ArrayList())

    var tempFiles: MutableList<UnUploadFile> = mutableListOf(
        UnUploadFile(
            stationId = 0, taskNo =
            "111", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 1, taskNo =
            "222", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 2, taskNo =
            "333", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        )
    )

    var tempFiles1: MutableList<UnUploadFile> = mutableListOf(
        UnUploadFile(
            stationId = 0, taskNo =
            "2111", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 1, taskNo =
            "2222", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 2, taskNo =
            "2333", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        )
    )

    var tempFiles2: MutableList<UnUploadFile> = mutableListOf(
        UnUploadFile(
            stationId = 0, taskNo =
            "3111", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 1, taskNo =
            "3222", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        ), UnUploadFile(
            stationId = 2, taskNo =
            "3333", filePath = "111", fileCreateTime = "2023年9月15日 15:49:37", isSelected = false
        )
    )

    fun loadDefaultUploadFile() {
        viewModelScope.launch {
            fileDao.delAll()
            fileDao.insert(
                UnUploadInfo(
                    projectName = "test1",
                    subName = "sub test1",
                    files = tempFiles
                )
            )
            fileDao.insert(
                UnUploadInfo(
                    projectName = "test2",
                    subName = "sub test2",
                    files = tempFiles1
                )
            )
            fileDao.insert(
                UnUploadInfo(
                    projectName = "test3",
                    subName = "sub test3",
                    files = tempFiles2
                )
            )
        }
    }

    fun getAllUnUploadFile() {
        viewModelScope.launch(Dispatchers.IO) {
            allFiles.emit(fileDao.filesList?.stateIn(viewModelScope)?.value ?: ArrayList())
        }
    }

    fun delSelectFiles() {
        viewModelScope.launch {
            allFiles.value.forEach {
                it.files.forEach { file ->
                    if (file.isSelected) {
                        //删除文件
                    }
                }
            }
            var allFilesNew = allFiles.value.map {
                var uploadFiles = it.files.asSequence().filter { !it.isSelected }.toMutableList()
                it.copy(files = uploadFiles)
            }

            allFiles.emit(allFilesNew.filter { it.files.size != 0 }.toMutableList())
        }
    }

    fun selectItem(unUploadFile: UnUploadFile) {
        val updatedList = allFiles.value.map { unUploadInfo ->
            val updatedFiles = unUploadInfo.files.map { file ->
                if (file == unUploadFile) {
                    file.copy(isSelected = !file.isSelected)
                } else {
                    file
                }
            }
            unUploadInfo.copy(files = updatedFiles.toMutableList())
        }
        allFiles.value = updatedList.toMutableList()
    }

    fun selectAll() {
        var allInfoNew =allFiles.value.map {
            var newFiles = it.files.map {
                it.copy(isSelected = true)
            }
            it.copy(files = newFiles.toMutableList())
        }
        allFiles.value = allInfoNew.toMutableList()
    }
}